<!-- 
  TransitionGroup 比 Transition 要多出一个类名：v-move 作用在周围移动的元素上

 -->

<template>
  <div>
    <h1>TransitionGroup</h1>

    <button @click="handleAdd">Add</button>
    <button @click="handleRemove">Remove</button>
    <button @click="handleSu">重排</button>
    <button @click="showDrawer = !showDrawer">Toggle Drawer</button>
    <hr />

    <TransitionGroup name="aaa" tag="div">
      <span class="item" v-for="item in list" :key="item">
        {{ item }}
      </span>
    </TransitionGroup>

    <Transition name="bbb">
      <div v-if="showDrawer" class="drawer"></div>
    </Transition>
  </div>
</template>

<script>
import _ from "lodash";

export default {
  data() {
    return {
      list: [1, 2, 3, 4, 5, 6],

      showDrawer: false,
    };
  },
  methods: {
    // 获取一个两数之间的随机整数，不包含 max
    getRandomInt(min, max) {
      min = Math.ceil(min);
      max = Math.floor(max);
      return Math.floor(Math.random() * (max - min)) + min; //不含最大值，含最小值
    },
    handleAdd() {
      // 在 list 的随机位置，添加一个数据
      //  1. 获取一个随机下标
      //      [0, 1)
      const index = this.getRandomInt(0, this.list.length);

      this.list.splice(index, 0, this.list.length + 1);
    },

    handleRemove() {
      const index = this.getRandomInt(0, this.list.length);
      this.list.splice(index, 1);
    },

    handleSu() {
      this.list = _.shuffle(this.list);
    },
  },
};
</script>

<style>
.item {
  width: 20px;
  height: 20px;
  text-align: center;
  display: inline-block;
}

.aaa-enter {
  transform: translateY(30px);
}

.aaa-enter-active {
  transition: transform 0.2s linear;
}

.aaa-leave-to {
  transform: translateY(-30px);
}

.aaa-leave-active {
  transition: transform 2s linear;
}

.aaa-move {
  transition: all 2s linear;
}

.drawer {
  position: fixed;
  top: 0;
  right: 0;
  width: 100px;
  height: 100%;
  background-color: green;
}

.bbb-enter,
.bbb-leave-to {
  transform: translateX(100px);
}
.bbb-enter-active,
.bbb-leave-active {
  transition: transform 0.3s ease;
}
</style>
